package soap.service.impl;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import soap.commons.ReturnCommons;
import soap.entity.bo.PersonBo;
import soap.entity.po.ExportRecord;
import soap.entity.po.User;
import soap.feign.dto.PersonDto;
import soap.feign.service.MSUserServiceFeign;
import soap.service.ExportRecordService;
import soap.service.PersonService;
import soap.threadpool.InitThreadPool;
import soap.utils.AuthUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by ZhangPY on 2020/6/7
 * Belong Organization OVERUN-9299
 * overun9299@163.com
 * Explain:
 */
@Service
public class PersonServiceImpl implements PersonService {


    @Autowired
    private MSUserServiceFeign mSUserServiceFeign;

    @Autowired
    private InitThreadPool initThreadPool;

    @Autowired
    private ExportRecordService exportRecordService;

    @Value("${filePath}")
    private String filePath;


    @Override
    public String getPersonListForExport(HttpServletRequest request, PersonDto personDto) {

        /** 定义导出记录 */
        ExportRecord exportRecord = new ExportRecord();
        exportRecord.setId(IdUtil.simpleUUID());
        exportRecord.setClassName("Person");
        exportRecord.setRequestDate(LocalDateTime.now());
        exportRecord.setState(0);
        /** 获取请求人id */
        User userFromRequest = AuthUtils.getUserFromRequest(request);
        exportRecord.setRequestorId(userFromRequest.getId().toString());

        exportRecordService.saveExportRecord(exportRecord);

        /** 定义bo */
        ThreadPoolExecutor executor = initThreadPool.getExecutor();
        PersonBo personBo = new PersonBo(mSUserServiceFeign, personDto , exportRecordService , exportRecord.getId(), filePath);
        executor.execute(personBo);

        return ReturnCommons.getSuccess(exportRecord.getId(), "请求成功,正在生成文件");
    }
}
